home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50b Issue 142 (CD142b) (August 1998).iso / handson / Delphi / ThPriorities / Thpr.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-04-11  |  2.5 KB  |  107 lines

  1. unit Thpr;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     ScrollBar1: TScrollBar;
  12.     ScrollBar2: TScrollBar;
  13.     ComboBox1: TComboBox;
  14.     Button1: TButton;
  15.     Button2: TButton;
  16.     procedure Button1Click(Sender: TObject);
  17.     procedure Button2Click(Sender: TObject);
  18.     procedure ComboBox1Change(Sender: TObject);
  19.     procedure FormCreate(Sender: TObject);
  20.   private
  21.     { Private declarations }
  22.   public
  23.     { Public declarations }
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.   thHandle1,
  29.   thHandle2,
  30.   ThIDOne,
  31.   ThIDTwo: integer;
  32.   thPriority : integer;
  33.  
  34. implementation
  35.  
  36. {$R *.DFM}
  37.  
  38. procedure Delay(Num : LongInt);
  39. // waste some time
  40. var
  41.   tc : LongInt;
  42. begin
  43.   tc := GetTickCount;
  44.   repeat
  45.   until ((GetTickCount-tc)>=Num);
  46. end;
  47.  
  48. function ScrollOne( P : Pointer ) : integer;
  49. begin
  50.    repeat
  51.     if Form1.ScrollBar1.Position = Form1.ScrollBar1.Max then
  52.       Form1.ScrollBar1.Position := Form1.ScrollBar1.Min
  53.     else Form1.ScrollBar1.Position := Form1.ScrollBar1.Position + 1;
  54.     Delay(100);
  55.    until 1 = 2;
  56. end;
  57.  
  58. function ScrollTwo( P : Pointer ) : integer;
  59. begin
  60.    repeat
  61.     if Form1.ScrollBar2.Position = Form1.ScrollBar2.Max then
  62.       Form1.ScrollBar2.Position := Form1.ScrollBar2.Min
  63.     else Form1.ScrollBar2.Position := Form1.ScrollBar2.Position + 1;
  64.     Delay(100);
  65.    until 1 = 2;
  66. end;
  67.  
  68. procedure TForm1.Button1Click(Sender: TObject);
  69. begin
  70.   Button1.Enabled := False; // don't try recreating these Threads!
  71.   ComboBox1.Enabled := False;
  72.   try
  73.      thHandle1 := BeginThread(nil,0,ScrollOne,nil,0,ThIDOne);
  74.      thHandle2 := BeginThread(nil,0,ScrollTwo,nil,0,ThIDTwo);
  75.      SetThreadPriority(thHandle1, thPriority);
  76.   finally
  77.      if thHandle1 <> 0 then
  78.         CloseHandle(thHandle1);
  79.      if thHandle2 <> 0 then
  80.         CloseHandle(thHandle2);
  81.   end;
  82. end;
  83.  
  84. procedure TForm1.Button2Click(Sender: TObject);
  85. begin
  86.   Close;
  87. end;
  88.  
  89. procedure TForm1.ComboBox1Change(Sender: TObject);
  90. begin
  91.   Case( ComboBox1.ItemIndex) of
  92.   0: thPriority := THREAD_PRIORITY_ABOVE_NORMAL ;
  93.   1: thPriority := THREAD_PRIORITY_BELOW_NORMAL ;
  94.   2: thPriority := THREAD_PRIORITY_HIGHEST ;
  95.   3: thPriority := THREAD_PRIORITY_IDLE ;
  96.   4: thPriority := THREAD_PRIORITY_NORMAL ;
  97.   5: thPriority := THREAD_PRIORITY_TIME_CRITICAL ;
  98.   end;
  99. end;
  100.  
  101. procedure TForm1.FormCreate(Sender: TObject);
  102. begin
  103.   thPriority := THREAD_PRIORITY_NORMAL ;
  104. end;
  105.  
  106. end.
  107.